export function parseApprovalNode(node, result = [], options = { addEndNode: true }) {
  if (!node) return result;
  
  // 处理当前节点
  const currentNode = {
    id: node.id,
    name: node.name,
    type: getNodeType(node.type),
    desc: node.content || '',
    approvers: [],
    selfSelect: false,
    conditions: []
  };
  
  // 处理审批人
  if (node.config) {
    if (node.config.assignedType === 'SELF_SELECT') {
      currentNode.selfSelect = true;
      currentNode.desc = '请选择审批人';
      currentNode.multiple = node.config.selfSelect.multiple === 'true';
    } else if (node.config.assignedUser) {
      currentNode.approvers = node.config.assignedUser.map(user => ({
        name: user.realName || user.username,
        avatar: user.avatar || '/static/images/default-avatar.png'
      }));
    }
  }
  
  result.push(currentNode);
  
  // 处理条件分支节点
  if (node.type === 4 && node.branchs) {
    // 保存条件分支信息
    currentNode.conditions = node.branchs.map(branch => ({
      id: branch.id,
      name: branch.name,
      desc: branch.content || branch.config.content || '',
      type: getNodeType(branch.type),
      childNode: branch.childNode,
      priorityLevel: branch.config.priorityLevel || 0
    }));
    
    // 递归所有分支的childNode
    node.branchs.forEach(branch => {
      if (branch.childNode) {
        parseApprovalNode(branch.childNode, result, { addEndNode: false });
      }
    });
    
    // 默认选中第一个分支（如有需要可保留）
    if (node.branchs[0]) {
      currentNode.currentBranch = node.branchs[0].id;
    }
  } else if (node.childNode) {
    // 递归处理子节点
    parseApprovalNode(node.childNode, result, { addEndNode: false });
  }
  
  // 只在最外层调用时添加结束节点
  if (options.addEndNode) {
    result.push({
      id: 'end',
      name: '流程结束',
      type: 'end',
      desc: '',
      approvers: []
    });
  }
  
  return result;
}

export function getBranchSteps(branch) {
  if (!branch) return [];
  
  const result = [];
  parseApprovalNode(branch.childNode, result, { addEndNode: true });
  return result;
}

function getNodeType(type) {
  switch (type) {
    case 0:
      return 'start';
    case 1:
      return 'approve';
    case 2:
      return 'copy';
    case 3:
      return 'branch';
    case 4:
      return 'condition';
    default:
      return 'end';
  }
} 